package com.xiaofan.realm;

import com.xiaofan.mapper.UserMapper;
import org.apache.catalina.realm.AuthenticatedUserRealm;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 自定义realm
 *
 * @author 晓帆
 * @version 1.0
 **/
@Component
public class CustomRealm extends AuthorizingRealm {
    @Autowired
    UserMapper userMapper;
    /**
     * 认证
     * @param authenticationToken 认证
     * @return AuthenticationInfo
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken= (UsernamePasswordToken) authenticationToken;
        String username = usernamePasswordToken.getUsername();
        String password="111";

        return new SimpleAuthenticationInfo(username,password,getName());


    }

    /**
     * 授权
     * @param principalCollection 授权
     * @return AuthorizationInfo
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String primaryPrincipal = (String) principalCollection.getPrimaryPrincipal();
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        //传入授权列表的集合，根据admin的roleId查询出对应的角色，通过角色的id查询出对应的权限
        simpleAuthorizationInfo.addObjectPermissions(null);

        return null;
    }


}
